
'use strict';

const Controller = require('egg').Controller;

class HouseAgent extends Controller {
    async index() {
        const { ctx } = this;
        const { payload } = ctx.query
        const obj = JSON.parse(payload)
        console.log(obj);
        const data = await this.app.mysql.query(`SELECT * FROM secondhouses WHERE prov LIKE '%${obj.prov}%' and city LIKE '%${obj.city}%' and district LIKE '%${obj.district}%' and cx LIKE '%${obj.cx}%' order by id asc`)
        let list = [...data]
        //筛选房型
        console.log(obj);
        console.log(list.length);
        if (obj.fangxing !== '') {
            if (payload.fangxing !== '三室+') {
                list = list.filter(v => v.fangxing.includes(obj.fangxing))
            } else {
                list = list.filter(v => v.fangxing.includes('四室') || v.fangxing.includes('五室') || v.fangxing.includes('五室以上'))
            }
        }
        //价格
        if (obj.jiage !== '') {
            list = list.filter(v => {
                return (v.jiage * 1) > (obj.jiage[0] * 1) && (v.jiage * 1) < (obj.jiage[1] * 1)
            })
        }
        console.log(list.length);
        //朝向

        ctx.body = {
            code: 200,
            data: list,
        };
    }
}

module.exports = HouseAgent;
